package com.moguizhuren.circlebarview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * Created by $Liudan on 2019/2/25 0025.
 */
public class CircleBarView extends View {

    private Paint rPaint;//绘制矩形的画笔
    private Paint progressPaint;//绘制圆弧的画笔

    private int progressColor;//进度条圆弧颜色
    private int bgColor;//背景圆弧颜色
    private float barWidth;//圆弧进度条宽度
    private float startAngle;//背景圆弧的起始角度
    private float sweepAngle;//圆弧经过的角度

    private float progressNum;//可以更新的进度条数值
    private float maxNum;//进度条最大值
    private CircleBarAnim anim;
    private int defaultSize ; //自定义View默认的宽高  px值

    public CircleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=measureDimension(defaultSize,widthMeasureSpec);
        int height=measureDimension(defaultSize,heightMeasureSpec);

        int min = Math.min(width, height);// 获取View最短边的长度
        //将计算的宽和高设置进去，保存，最后一步一定要有
//        setMeasuredDimension(width,height);
        setMeasuredDimension(min, min);// 强制改View为以最短边为长度的正方形
    }

    private void init(Context context, AttributeSet attrs){
        defaultSize = DpOrPxUtils.dip2px(context,300);
        barWidth = DpOrPxUtils.dip2px(context,10);

        rPaint = new Paint();

        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CircleBarView);

        progressColor = typedArray.getColor(R.styleable.CircleBarView_progress_color,Color.GREEN);//默认为绿色
        bgColor = typedArray.getColor(R.styleable.CircleBarView_bg_color,Color.GRAY);//默认为灰色
        startAngle = typedArray.getFloat(R.styleable.CircleBarView_start_angle,0);//默认为0
        sweepAngle = typedArray.getFloat(R.styleable.CircleBarView_sweep_angle,360);//默认为360
        barWidth = typedArray.getDimension(R.styleable.CircleBarView_bar_width,DpOrPxUtils.dip2px(context,10));//默认为10dp
        typedArray.recycle();//typedArray用完之后需要回收，防止内存泄漏


        rPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
        rPaint.setColor(Color.RED);

        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.STROKE);//只描边，不填充
        progressPaint.setColor(progressColor);
        progressPaint.setAntiAlias(true);//设置抗锯齿
        progressPaint.setStrokeWidth(barWidth); //画笔宽度

        maxNum = 100;

        anim = new CircleBarAnim();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float x = 50;
        float y = 50;
        RectF rectF = new RectF(x,y,x+300,y+300);//建一个大小为300 * 300的正方形区域

//        canvas.drawArc(rectF,0,270,false,progressPaint);//这里角度0对应的是三点钟方向，顺时针方向递增
        canvas.drawRect(rectF,rPaint);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,progressPaint);
    }

    public class CircleBarAnim extends Animation {

        public CircleBarAnim(){
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            sweepAngle = interpolatedTime * 360 * progressNum / maxNum;//这里计算进度条的比例;
            postInvalidate();
        }
    }

    //写个方法给外部调用，用来设置动画时间
    public void setProgressNum(float progressNum ,int time) {
        this.progressNum = progressNum;
        anim.setDuration(time);
        this.startAnimation(anim);
    }

    /**
     * @param defualtSize   设置的默认大小
     * @param measureSpec   父控件传来的widthMeasureSpec，heightMeasureSpec
     * @return  结果
     */
    public int measureDimension(int defualtSize,int measureSpec){
        int result=defualtSize;
        int specMode=MeasureSpec.getMode(measureSpec);
        int specSize=MeasureSpec.getSize(measureSpec);

        //1,layout中自定义组件给出来确定的值，比如100dp
        //2,layout中自定义组件使用的是match_parent，但父控件的size已经可以确定了，比如设置的具体的值或者match_parent
        if(specMode==MeasureSpec.EXACTLY){
            result=specSize;
        }
        //layout中自定义组件使用的wrap_content
        else if(specMode==MeasureSpec.AT_MOST){
            result=Math.min(defualtSize,specSize);//建议：result不能大于specSize
        }
        //UNSPECIFIED,没有任何限制，所以可以设置任何大小
        else {
            result=defualtSize;
        }
        return result;
    }
}